package com.turing.usdtpay.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

import com.google.gson.Gson;
import com.turing.usdtpay.entity.MoneyData;
import com.turing.usdtpay.entity.OrderInfoData;
import com.turing.usdtpay.entity.RobData;
import com.turing.usdtpay.http.ApiService;
import com.turing.usdtpay.http.ApiUrl;
import com.turing.usdtpay.http.callback.ApiCallback;
import com.turing.usdtpay.http.entity.ResponseDate;
import com.turing.usdtpay.mvp.contract.RobOrderContract;
import com.turing.usdtpay.service.PushService;
import com.turing.usdtpay.utils.LogUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * @作者：JAY
 * @创建时间：2019/10/21 17:43
 * @描述：
 **/
public class RobOrderPresenter implements RobOrderContract.Present {
    private String TAG = "RobOrderPresenter";
    private RobOrderContract.View mView;
    private Intent intent;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.d(TAG, "msg.what :" + msg.what + ",obj ==" + (String) msg.obj);
            mView.serviceResult(msg.what);
        }
    };

    public RobOrderPresenter(RobOrderContract.View mView) {
        this.mView = mView;
    }

    @Override
    public void getState(Activity activity) {
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_ROB_HOME_PAGE, new HashMap<String, String>(), new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1) {
                    Gson gson = new Gson();
                    RobData robData = gson.fromJson(date.getDate(), RobData.class);
                    mView.getStateSuccess(robData);
                } else {
                    mView.onFail(date.getMsg());
                }

            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    @Override
    public void buyOrder(final Activity activity) {
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_START_BUY, new HashMap<String, String>(), new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1) {
                    mView.buying();
                    pushNotification(activity);
                } else {
                    mView.onFail(date.getMsg());
                }
            }
            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }


    @Override
    public void sellOrder(final Activity activity) {
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_START_SELL, new HashMap<String, String>(), new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1) {
                    try {
                        JSONObject object = new JSONObject(date.getDate());
                        String sell_info = object.optString("sell_info");
                        mView.selling(sell_info);
                        pushNotification(activity);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });

    }


    @Override
    public void stopBuy(Activity activity) {
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_STOP_TRADE, new HashMap<String, String>(), new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1) {
                    mView.stopTrader(1);
                } else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    @Override
    public void stopSell(Activity activity) {
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_STOP_TRADE, new HashMap<String, String>(), new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1) {
                    mView.stopTrader(2);
                } else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    @Override
    public void getOrderInfo(Activity activity, final int type) {
        HashMap<String, String> map = new HashMap<>();
        map.put("order", "0");
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_ORDER_INFO, map, new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1) {
                    Gson gson = new Gson();
                    OrderInfoData data = gson.fromJson(date.getDate(), OrderInfoData.class);
                    mView.getOrder(data, type);
                } else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    @Override
    public void startServer(Context context) {
        starService(context);
    }

    @Override
    public void getMoney(Activity activity) {
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_MONEY_HOME_PAGE, new HashMap<String, String>(), new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    Gson gson = new Gson();
                    MoneyData data = gson.fromJson(date.getDate(), MoneyData.class);
                    int money = data.getMoney();
                    mView.getMoneySuccess(money);
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    private void starService(Context context) {
        if (intent == null) {
            intent = new Intent(context, PushService.class);
        }
        LogUtils.d("starService");
        intent.putExtra("msg", new Messenger(handler));
        context.startService(intent);
    }

    private void pushNotification(Context context) {
        if (intent == null) {
            intent = new Intent(context, PushService.class);
        }
        LogUtils.d("starService");
        intent.putExtra("push",true);
        intent.putExtra("msg", new Messenger(handler));
        context.startService(intent);
    }
}
